<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\admin\model\Comment;
use app\common\model\Category as CategoryModel;
use \app\admin\model\LitestoreGoods;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['banner','category','goods_list','goods_detail','get_child','get_rent_catelist','rent_good_list','search'];
    protected $noNeedRight = ['*'];
    protected $LitestoreGoods = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 1;
        $this->LitestoreGoods = new \app\admin\model\LitestoreGoods;
    }  


    /**
     * 轮播图
     * @ApiSummary  ()
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/banner)
     * @ApiReturn   ({
        
        })
     */
      public function banner(){
        $return = Db::name('banner')->order('weigh desc')->select();
        $this->success('轮播图',$return);
      }


    /**
     * 分类列表
     * @ApiSummary  ()
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/category)
     */
    public function Category(){
      $return = Db::name('category')->where('type','Index')->select();
      $this->success('分类列表',$return);
    }

     /**
     * 商品列表
     * @ApiSummary  (spec下的stock_num是库存，sale_num是已卖出的数量)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_list)
     * @ApiReturn ({
       
     })
     */
    public function goods_list(){
        
        $row = $this->LitestoreGoods
        ->with(['specRel', 'spec', 'spec_rel.spec'])
        ->where(['type'=>'Index','good_status'=>1])
        ->order('weigh desc')
        ->select();

        $this->success('商品列表',$row);
    }

     /**
     * 搜索商品
     * @param string $goods_name 
     */
    public function search(){
        $goods_name = input('goods_name');

        $row = $this->LitestoreGoods
        ->with(['specRel', 'spec', 'spec_rel.spec'])
        ->where(['type'=>'Index','good_status'=>1])
        ->where(['goods_name'=>['LIKE',"%$goods_name%"]])
        ->order('weigh desc')
        ->select();
        
        $this->success('搜索商品',$row);
    }

     /**
     * 商品详情页面
     * @ApiSummary  (spec_type 20:多规格/10:单规格。)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_detail)
     * @ApiParams   (name="goods_id",type=int,required=true,description="商品id")
     * @ApiReturn ({
       
     })
     */
    public function goods_detail(){
        $goods_id = $this->request->request('goods_id');
        if(empty($goods_id))$this->error('缺少商品id');
        
        $LitestoreGoods = new LitestoreGoods;
        $detail = $LitestoreGoods->detail($goods_id, ['specRel', 'spec', 'spec_rel.spec']);
        if (!$detail)$this->error('很抱歉，商品信息不存在');
        // 规格信息
        $specData= $LitestoreGoods->getApiManySpecData($detail['spec_rel'], $detail['spec']);

        $detail = $detail->toArray();
        if($detail['spec_type']=='20'){
          unset($detail['spec_rel']);
          // unset($detail['spec']);
       
          $data = array_merge($detail,$specData);
        }else{

          $data = $detail;
        }
        
        $this->success('商品详情页面',$data);

    }



}
